除非你是在使用CRichEditView,否则你必须在显示该对话框之前调用AfxInitRichEdit初始化RichEditCtrl的运行环境,通常是在你的应用程序的IniInstance成员函数中调用这个函数。
AfxInitRichEdit();
可在InitInstance中添加
LoadLibrary("RICHED20.DLL")
最后注意 FreeLibrary 如果是CRichEditView基类的可用
BOOL CXXXXXXView::PreCreateWindow(CREATESTRUCT& cs) { //装入rich edit version 2.0 if (LoadLibraryA("RICHED20.DLL") == NULL) { AfxMessageBox(_T("Fail to load "riched20.dll"."),MB_OK | MB_ICONERROR); PostMessage(WM_QUIT,0,0); return FALSE; } m_strClass = RICHEDIT_CLASSA;//for 2.0 class return CRichEditView::PreCreateWindow(cs); }
richeditctrl.SetSel(-1, -1); //将光标移动到最后
richeditctrl.ReplaceSel( (LPCTSTR)str );
CRichEditCtrl::LimitText(long nChars)
CRichEditView的OnInitialUpdate()函数中加入下面两句:
m_nWordWrap = WrapNone;
WrapChanged();
WrapChanged实际上也是调用
ctrl.SetTargetDevice(NULL, 1); //m_nWordWrap == WrapNone
ctrl.SetTargetDevice(NULL, 0); //m_nWordWrap == WrapToWindow
还有不常用的 m_nWordWrap == WrapToTargetDevice
ctrl.SetTargetDevice(m_dcTarget, GetPrintWidth());
如果是在Dialog中,可使用SetTargetDevice,注意在属性里面加上want return
pmyRichEditCtrl->;PasteSpecial(CF_TEXT);
int nFirstVisible = pmyRichEditCtrl->GetFirstVisibleLine(); if (nFirstVisible > 0) { pmyRichEditCtrl->LineScroll(-nFirstVisible, 0); }或
m_cRichEdit.PostMessage(WM_VSCROLL, SB_BOTTOM,0);
SendMessage(EM_SETTEXTMODE,TM_MULTILEVELUNDO,0);
TM_MULTILEVELUNDO 支持多取消(默认值).可通过EM_SETUNDOLIMIT设置最大次数
SendMessage(EM_SETUNDOLIMIT,100,0);
EM_SETEVENTMASK 设置 ENM_CHANGE
long lMask = GetEventMask();
lMask |= ENM_CHANGE;
lMask &= ~ENM_PROTECTED;
SetEventMask(lMask);
CRichEditCtrl::SetReadOnly( BOOL bReadOnly = TRUE );
CHARFORMAT cf; ZeroMemory(&cf, sizeof(CHARFORMAT)); cf.cbSize = sizeof(CHARFORMAT); cf.dwMask|=CFM_BOLD; cf.dwEffects|=CFE_BOLD;//设置粗体,取消用cf.dwEffects&=~CFE_BOLD; cf.dwMask|=CFM_ITALIC; cf.dwEffects|=CFE_ITALIC;//设置斜体,取消用cf.dwEffects&=~CFE_ITALIC; cf.dwMask|=CFM_UNDERLINE; cf.dwEffects|=CFE_UNDERLINE;//设置斜体,取消用cf.dwEffects&=~CFE_UNDERLINE; cf.dwMask|=CFM_COLOR; cf.crTextColor = RGB(255,0,0);//设置颜色 cf.dwMask|=CFM_SIZE; cf.yHeight =200;//设置高度 cf.dwMask|=CFM_FACE; strcpy(cf.szFaceName ,_T("隶书"));//设置字体 rich.SetSelectionCharFormat(cf);
要用richedit2.0以上
PARAFORMAT2 pf; pf.cbSize = sizeof(PARAFORMAT2); pf.dwMask = PFM_NUMBERING | PFM_OFFSET; pf.wNumbering = PFN_BULLET;//注意PFM_NUMBERING pf.dxOffset = 10; VERIFY(SetParaFormat(pf));
常用的dwMask有
PFM_NUMBERING 成员 wNumbering 才起作用,项目符号,
默认用PFN_BULLET
2 使用阿拉伯数字 (1, 2, 3, ...).
3 使用小写字母 (a, b, c, ...).
4 使用大写字母 (A, B, C, ...).
5 使用小写罗马数字 (i, ii, iii, ...).
6 使用大写罗马数字 (I, II, III, ...).
7 自定义,字符见成员 wNumberingStart.
PFM_OFFSET 成员 dxOffset 才起作用,缩进,单位twips
PFM_STARTINDENT 成员 dxStartIndent 才起作用,首行缩进
PFM_SPACEAFTER 成员 dySpaceAfter 才起作用,段间距
PFM_LINESPACING 成员 dyLineSpacing 才起作用,行间距
long style = ::GetWindowLong(GetSafeHwnd(), GWL_EXSTYLE); style &= WS_EX_TRANSPARENT; ::SetWindowLong(GetSafeHwnd(), GWL_EXSTYLE, style);
或 CreateEx,然后把WS_EX_TRANSPARENT样式加上
1)GetWindowText
2)使用EM_GETTEXTEX
GETTEXTEX gt; gt.cb = 200; gt.flags = GT_DEFAULT; gt.codepage = CP_ACP ; gt.lpDefaultChar = NULL; gt.lpUsedDefChar = NULL; SendMessage(EM_GETTEXTEX,(WPARAM)>,(LPARAM)text);
3)StreamOut(主要用于RTF等格式输出)
static DWORD CALLBACK MyStreamOutCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb) { CFile* pFile = (CFile*) dwCookie; pFile->Write(pbBuff, cb); *pcb = cb; return 0; } CFile cFile(TEXT("myfile.rtf"), CFile::modeCreate|CFile::modeWrite); EDITSTREAM es; es.dwCookie = (DWORD) &cFile;//设置用例参数,以便回调函数调用 es.pfnCallback = MyStreamOutCallback; pmyRichEditCtrl->StreamOut(SF_RTF, es);
读入可以此类推,SetWindowText,EM_SETTEXTEX,StreamIn
FINDTEXTEX ft; ft.chrg.cpMin = 0; ft.chrg.cpMax = -1; ft.lpstrText = "|"; long lPos = FindText(0, &ft); 如果要继续查找,修改cpMin,如 int nCount = 0; do { long lPos = GetRichEditCtrl().FindText(0, &ft); if( -1 == lPos) break; ft.chrg.cpMin = lPos + strlen(ft.lpstrText); ++nCount; }while(TRUE);
void CMYichEditorView::OnProtected(NMHDR* pNMHDR, LRESULT* pResult) { ENPROTECTED* pEP = (ENPROTECTED*)pNMHDR; switch (pEP->msg) { case WM_KEYDOWN://按键,判断pEP->wParam case WM_PASTE://粘贴 case WM_CUT://剪切 case EM_SETCHARFORMAT: default: break; }; *pResult = FALSE; }
1. LoadLibrary(_T("Riched20.dll"));
2. 创建RichEdit2.0控件
CreateEx(0, _T("RichEdit20A"), NULL, WS_CHILD|WS_VISIBLE|WS_VSCROLL|WS_TABSTOP |ES_READONLY|ES_WANTRETURN|ES_MULTILINE, rect.left, rect.top, cx, cy, pParentWnd->m_hWnd, (HMENU)nID, NULL);
3. 设定选中的文字为链接显示
CHARFORMAT2 cf2; ZeroMemory(&cf2, sizeof(CHARFORMAT2));// cf2.cbSize = sizeof(CHARFORMAT2); cf2.dwMask = CFM_LINK; cf2.dwEffects |= CFE_LINK; m_cRichEdit.SendMessage(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf2);
m_cRichEdit.SetEventMask(ENM_LINK);
EN_LINK BEGIN_MESSAGE_MAP(CMyRichEdit, CRichEditCtrl) ON_NOTIFY_REFLECT(EN_LINK,OnURL) END_MESSAGE_MAP() ...... void CMyRichEdit::OnURLClick(NMHDR *pNmhdr, LRESULT *pResult) { TCHAR LinkChar[512]; ENLINK *pLink = (ENLINK *)pNmhdr; if (pLink->msg == WM_LBUTTONUP) { SetSel(penLink->chrg);//这是链接的文字范围 long Res = GetSelText((char *)LinkChar);//这是链接文字 //后面是你的处理过程 ...... } }
本页共211段,6276个字符,7684 Byte(字节)